package thread.threadPool;

import java.util.concurrent.*;

/**
 * My implementation of JUC Executors which is a factory utility for ThreadPoolExecutors
 */
public class MyExecutors {
    static int blockingQueueCapacity;

    public static boolean setWorkQueueCapacity(int cap) {
        if (cap > 0) {
            blockingQueueCapacity = cap;
            return true;
        }
        return false;
    }

    public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
        if (blockingQueueCapacity != Integer.MAX_VALUE) {
            return new ThreadPoolExecutor(nThreads, nThreads,
                    0L, TimeUnit.MILLISECONDS,
                    new LinkedBlockingQueue<Runnable>(blockingQueueCapacity),
                    threadFactory);
        }else {
            throw new UnbounedBlockingQueueException();
        }
    }
}
